import { ref } from "vue"
//导入Http请求
import { Http } from "../../../http/users"
//导入store获得id
import { useUserStore } from "@/stores/users"
import router from "../../../router"
const { user } = useUserStore()
import { Toast } from "vant"
// console.log(user.id)
export const useUserMsg = () => {
  //用户退出登录
  async function exitUser() {
    //发送请求,用户下线
    let data = await Http.underLine(user.id)
    if (data.code == 200) {
      //清除token,退出登录
      user.token = ""

      Toast.success("退出成功")
      router.push("/")
    }
  }
  //用户注销
  async function logOut() {
    //发送请求注销用户
    let data = await Http.deleteUsers(`${user.id}`)
    if (data.code == 200) {
      //清除token,用户注销
      user.token = ""

      Toast.success("注销成功")
      router.push("/")
    }
  }
  //控制修改弹框显隐
  const showEdit = ref(false)
  //修改用户信息
  const username = ref(user.username)
  const password = ref(user.password)
  const onSubmit = async (values) => {
    //发送请求修改信息
    let data = await Http.updateUsers(user.id, {
      username: username.value,
      password: password.value,
      type: user.type,
    })
    //修改成功,返回首页重新登录
    if (data.code == 200) {
      user.token = ""

      Toast.success("修改信息成功,请重新登录")
      router.push("/")
    }
  }
  const onFailed = (errorInfo) => {
    Toast.fail("提交信息失败")
  }
  return { exitUser, logOut, showEdit, username, password, onSubmit, onFailed }
}
